<?php
namespace Home\Model;
//物品基础属性
class Warehouse extends Container{
    protected $redis;
    protected $user;
    protected $type ;
    protected $index ;
    protected $username ;
    
    public function __construct($user){
        //数据集
        parent::__construct($user, 'warehouse');
    }

    //初始化有10个储存
    public function initData(){
       $this->addBox(1);
       $this->addBox(2);
       $this->addBox(3);
    }

    public function addBox($index){
        if($index < 1 || $index >9) return error('只能有九个储物箱');
        $shop = new \Home\Model\Shop($this->user);
        $old = $shop->get('box'.$index);
        if($old) return error('改储物箱已存在');
        if(!$shop->add('box', true)) return error('商店没有足够的空间!');
        $shop->add('box');
        $this->up(5);
    }

    //加物品,检查总空间是否足够
    public function add($name, $level, $amount, $pre = false){
        $max = $this->getMax();
        if($max['current'] + $amount > $max['max']) return false;
        if($pre) return true;
        return parent::add($name.'_'.$level, $amount);
    }
    //减物品
    public function dec($name, $level, $amount, $pre = false){
        if($level){
            if(checkLock($name, $level) == true) return error('物品已锁定');
            return parent::dec($name.'_'.$level, abs($amount), $pre);
        }else{
            //减任务品
            $num = $this->hGet($name);
            if(!$num || $num < $amount)
                return false;
            if($pre) return true;
            return $this->hIncrBy($name, $amount);
        }
    }
    //忽略品质减物品
    public function decAll($name, $base_level, &$max_level, $amount, $pre = false){
        $count = $amount;
        $task = array();
        for($i = $base_level; $i <= 15; $i++){
            $tmp = $this->getUnlock($name, $i);
            if($tmp){
                $task[] = array('level'=>$i, 'amount'=>$tmp>$count? $count: $tmp);
                $count -= $tmp;
                if($count <= 0)break;
            }
        }
        if($count <= 0){
            //数量足够了
            if($i > $max_level){
                //超出预计品质, 返回失败并更新最大品质
                $max_level = $i;
                return false;
            }
            if($pre) return true;
            foreach($task as $key=>$value){
                $this->dec($name, $value['level'], $value['amount']);
            }
        }
        if($pre) return false;
        return error('没有足够的物品:'.$name);
    }

    public function get($name, $level){
        return $this->hGet($name.'_'.$level);
    }
    public function getUnlock($name, $level){
        if(checkLock($name, $level) == true) return 0;
        return $this->hGet($name.'_'.$level);
    }
    public function checkLock($name, $level){
        return($this->hGet($name.'_'.$level.'_lock') == '1'? true: false);
    }
    public function lock($name, $level){
        $this->hSet($name.'_'.$level.'_lock', '1');
    }
    public function unlock($name, $level){
        $this->hdel($name.'_'.$level.'_lock');
    }

    public function like($name, $level){
        $this->hSet($name.'_'.$level.'_like', '1');
    }
    public function unlike($name, $level){
        $this->hdel($name.'_'.$level.'_like');
    }
} 